const app = getApp()
import { townNameDynamic, selectCtionActivity, collectionNum,activityOption} from "../../../api/homePage"
import { baseUrl } from "../../../api/request"
import { getStorage } from "../../../utils/localStorage"
Page({
    data: {
        navData:{
            level:2,
            title:"乡镇活动",
        },
        user: null,
        baseUrl: null,
        list: [],
        listQuery: {
            pageNum: 1,
            pageSize: 20,
            townShipName: "",
            total: 0,
            pullFlag: true,
        },
    },
    onShow() {

    },
    onLoad(options) {
        let item = JSON.parse(decodeURIComponent(options.item));
        this.setData({
            "listQuery.townShipName": item,
        });
        console.log(item,'家乡活动');
        this.setValue();
        this.getList();
    },
    onReady() {

    },
    //初始化页面值
    setValue() {
        this.setData({
            user: getStorage('user'),
            baseUrl: baseUrl,
        });
    },
    //获取列表数据
    async getList() {
        let _this = this.data;
        let arr = _this.list;
        if (_this.listQuery.pullFlag) {
            let res = await townNameDynamic(_this.listQuery);
            let { code, rows, total } = res.data;
            if (code === 200) {
                rows.forEach(async item => {
                    item.medies = item.medies.split(",");
                    if (scaNum.data.code === 200) {
                        item.colleNum = scaNum.data.data;
                        arr.push(item);
                        this.setData({
                            list: arr,
                        });
                    };
                });
                this.setData({
                    "listQuery.total": Math.ceil(total / _this.listQuery.pageSize),  //总页数
                });
            };
        };
    },
    //活动收藏图标事件
    async collectionClick(e) {
        let _this = this.data;
        let index = e.currentTarget.dataset.index;
        let obj =  e.currentTarget.dataset.item;
        let status = obj.colleStatus === 0 ? 1 : obj.colleStatus === 1 ? 0 : 0;
        let res = await activityOption({ userId: this.data.user.id, activityId: obj.id, status: status });
        if (res.data.code === 200) {
            let colleStatus = "list[" + index + "].colleStatus";
            let colleNum = "list[" + index + "].colleNum";
            let scNum = 0;
            if (res.data.data === 1) {
                scNum = _this.list[index].colleNum + 1;
            } else {
                scNum = _this.list[index].colleNum - 1;
            };
            this.setData({
                [colleStatus]: res.data.data,
                [colleNum]:scNum,
            });
        };
    },
    //图片错误时
    imgError(e){
        console.log(e,'有图显示不出来');
    },
    //上拉加载更多
    onReachBottom() {
        let _this = this.data;
        if (_this.listQuery.pageNum < _this.listQuery.total) {
            let index = _this.listQuery.pageNum + 1;
            this.setData({
                "listQuery.pageNum": index,
            });
            this.getList();
        } else {
            this.setData({
                "listQuery.pullFlag": false,
            });
            wx.showToast({
                icon: 'none',
                title: '已经到底了',
            })
        };
    },
})